package spacegame;

// Reference implementation of Coord ADT
//
//   CS 3500 -- Fall 2009

public class Coord {

    private int xpos;
    private int ypos;

    private Coord (int x, int y) {
	xpos = x;
	ypos = y;
    }

    public static Coord create (int x, int y) {
	if (x < 0 || y < 0)
	    throw new IllegalArgumentException 
		("negative arguments not allowed for Coord.create()");
	return new Coord(x,y);
    }

    public static Coord origin () {
	return create(0,0);
    }

    public int xPos () {
      return this.xpos;
    }

    public int yPos () {
	return this.ypos;
    }

    public Coord move (int x, int y) {
	return create(this.xPos()+x, this.yPos()+y);
    }

    public Coord add (Coord c) {
      return this.move (c.xPos(), c.yPos());
    }

    public boolean within (Coord c1, Coord c2) {
	return (c1.xPos() <= this.xPos() & 
		c1.yPos() <= this.yPos() &
		this.xPos() <= c2.xPos() &
		this.yPos() <= c2.yPos());
    }

    public double distance (Coord c) {
	return Math.sqrt(Math.pow(this.xPos()-c.xPos(),2) + 
			 Math.pow(this.yPos()-c.yPos(),2));
    }


    public boolean equals (Coord c) {
	return (this.xPos()==c.xPos() && this.yPos()==c.yPos());
    }

    
  public boolean equals (Object obj) {
    return false;
  }

    public int hashCode () {
	return (this.xPos()+this.yPos())*(this.xPos()+this.yPos()+1)/2 + 
	        this.yPos();
    }


    public String toString () {
	return "("+this.xPos()+","+this.yPos()+")";
    }

}
